<?php

/**
 * wiki推荐语数据更新(更新wiki是否设计师推荐)
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Doraemon\cache\BlackList;
use Doraemon\model\AdmUserType as mAdmUserType;
use Doraemon\model\Article as mArticle;
use Doraemon\model\Blank as mBlank;
use Doraemon\model\Member as mMember;
use Doraemon\model\Photos as mPhotos;
use Doraemon\tools\BlockType;
use Doraemon\tools\IdParse;

$page = 1;
$pageSize = 100;
$uids = mMember::select('uid', ['type' => mMember::AUTH_USER_DESIGNER, 'status' => mMember::STATUS_NORMAL]);
$uids = BlackList::filterBlackUser($uids);//过滤B级用户
$blevesUidsArr = mAdmUserType::getBlevelsByuids($uids);
$blevesUids = [];
foreach ($blevesUidsArr as $uid => $row) {
    if (count($row) > 0 && $row) {
        $blevesUids[] = $uid;
    }
}
$uids = array_diff($uids, $blevesUids);
$list = [];
do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    var_dump("查询已关联wiki的内容列表", $limit);
    $list = mObjRelateWiki::getByWhere(['uid' => $uids, 'status' => mObjRelateWiki::STATUS_NORMAL, 'LIMIT' => $limit], ['uid', 'obj_id', 'obj_type', 'wiki_id']);
    var_dump('查询已关联wiki的内容：' . count($list) . '条');
    if ($list) {
        $objWikiIds = [];
        foreach ($list as $row) {
            $objId = $row['obj_id'];
            $objType = $row['obj_type'];
            $wikiId = $row['wiki_id'];
            $uid = $row['uid'];

            if (!isset($objWikiIds[$objId])) {
                $objWikiIds[$objId] = [];
            }
            if (!in_array($wikiId, $objWikiIds[$objId])) {
                $objWikiIds[$objId][] = $wikiId;
            }
        }
        if ($objWikiIds) {
            var_dump('开始更新wiki是否设计师分享');
            changeDesignerShare($objWikiIds);
        }
    }
    $page++;
} while ($list);


function changeDesignerShare($objWikiIds)
{
    foreach ($objWikiIds as $objId => $wikiIds) {
        $objType = IdParse::getType($objId);
        if ($objType == BlockType::TYPE_PHOTO || $objType == BlockType::TYPE_ANSWER) {
            $info = mPhotos::getPhoto($objId);
            if ($info && $info['admin_score'] > 10) {
                mWiki::updateData(['designer_share' => 1], ['id' => $wikiIds, 'designer_share' => 0]);
            }
        } else if ($objType == BlockType::TYPE_ARTICLE) {
            $info = mArticle::getArticleInfo($objId);
            if ($info && $info['admin_score'] > 10) {
                mWiki::updateData(['designer_share' => 1], ['id' => $wikiIds, 'designer_share' => 0]);
            }
        } else if ($objType == BlockType::TYPE_BLANK) {
            $info = mBlank::getBlank($objId);
            if ($info && $info['admin_score'] > 10) {
                mWiki::updateData(['designer_share' => 1], ['id' => $wikiIds, 'designer_share' => 0]);
            }
        }
    }
}